﻿using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.common.unit;
using EkStudio.com.ui.unitbill.property.factory;
using System;
using System.ComponentModel;
using System.Reflection;

namespace EkStudio.com.ui.unitbill.property
{

    /// <summary>
    /// 字段转属性配置
    /// </summary>
    public class BillUiFieldToProperty:ObjectToProperty
    {

        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillUiFieldToProperty));

        private BillUiGroupItems Table = null;

        private BillUiFieldsItem item = null;
        private BillUiFieldToProperty[] array = null;


        private bool simple = true;

        public BillUiFieldToProperty(BillUiFieldsItem field,BillUiGroupItems table)
        {
            this.item = field;
            this.Table = table;
            Simple = true;
        }

        public BillUiFieldToProperty(BillUiFieldToProperty[] list)
        {
            Array = list;
            Simple = false;
        }

        public void setField(BillUiFieldToProperty target)
        {
            if (target.Simple)
            {
                item = target.FieldItem;
                Simple = true;
            }
            else
            {
                Array = target.Array;
                Simple = false;
            }
        }


        #region 数据属性
        public string Key
        {
            get { return item != null ? item.Key : GetStringValue("Key"); }
            set { if (item != null) item.Key = value; else SetValue("Key", value); ActiveEvent("Key", value); }
        }
        public string Caption
        {
            get { return item != null ? item.Caption : GetStringValue("Caption"); }
            set { if (item != null) item.Caption = value; else SetValue("Caption", value); }
        }
        public string EditType
        {
            get { return item!=null?item.DataProperty.EditType:GetStringValue("EditType"); }
            set { if (Simple) item.DataProperty.EditType = value; else SetValue("EditType", value); ActiveEvent("EditType", value); }
        }

        public string ComboType
        {
            get { return Simple?item.DataProperty.ComboType:GetStringValue("ComboType"); }
            set { if (Simple) item.DataProperty.ComboType = value; else SetValue("ComboType", value);}
        }

        public bool MultiSelect
        {
            get { return Simple?item.DataProperty.MultiSelect:GetBoolValue("MultiSelect"); }
            set { if(Simple)item.DataProperty.MultiSelect = value; else SetValue("MultiSelect", value); ActiveEvent("MultiSelect", value); }
        }

        public string DataType
        {
            get { return Simple ? item.DataProperty.DataType : GetStringValue("DataType"); }
            set { if (Simple) item.DataProperty.DataType = value; else SetValue("DataType", value); ActiveEvent("DataType", value); }
        }

        public int Size
        {
            get { return Simple ? item.DataProperty.Size : GetIntValue("Size") ; }
            set { if(Simple)item.DataProperty.Size = value; SetValue("Size", value); ActiveEvent("Size", value);}
        }

        public string DataSource
        {
            get { return Simple?item.DataProperty.DataSource:GetStringValue("DataSource"); }
            set { if (Simple) item.DataProperty.DataSource = value; else SetValue("DataSource", value); }
        }
        public bool FnNotNull
        {
            get { return Simple?item.DataProperty.FnNotNull:GetBoolValue("FnNotNull"); }
            set { if (Simple) item.DataProperty.FnNotNull = value; else SetValue("FnNotNull", value); }
        }
        public bool FnDistinct
        {
            get { return Simple?item.DataProperty.FnDistinct:GetBoolValue("FnDistinct"); }
            set { if (Simple) item.DataProperty.FnDistinct = value; else SetValue("FnDistinct", value); }
        }
        [Description("虚拟字段的数据源不更新到表结构中")]
        public bool VirtualType
        {
            get { return Simple ? item.DataProperty.VirtualType : GetBoolValue("VirtualType"); }
            set { if (Simple) item.DataProperty.VirtualType = value; else SetValue("VirtualType", value); }
        }

        public string DtFormat
        {
            get { return Simple?item.DataProperty.DtFormat:GetStringValue("DtFormat"); }
            set { if (Simple) item.DataProperty.DtFormat = value; else SetValue("DtFormat", value);}
        }
        public string Memory
        {
            get { return Simple?item.DataProperty.Memory:GetStringValue("Memory"); }
            set { if (Simple) item.DataProperty.Memory = value; else SetValue("Memory", value);  }
        }

        #endregion

        #region 编辑属性
        public bool Enable
        {
            get { return Simple?item.EditProperty.Enable:GetBoolValue("Enable"); }
            set { if (Simple) item.EditProperty.Enable = value; else SetValue("Enable", value);  }
        }
        public bool Visible
        {
            get { return Simple?item.EditProperty.Visible:GetBoolValue("Visible"); }
            set { if (Simple) item.EditProperty.Visible = value; else SetValue("Visible", value); }
        }
        public bool ReadOnly
        {
            get { return Simple?item.EditProperty.ReadOnly:GetBoolValue("ReadOnly"); }
            set { if (Simple) item.EditProperty.ReadOnly = value; else SetValue("ReadOnly", value); }
        }
        public string CharCase
        {
            get { return Simple ? item.EditProperty.CharCase : GetStringValue("CharCase"); }
            set { if (Simple) item.EditProperty.CharCase = value; else SetValue("CharCase", value);  }
        }
        public bool PasswordType
        {
            get { return Simple ? item.EditProperty.PasswordType : GetBoolValue("PasswordType"); }
            set { if (Simple) item.EditProperty.PasswordType = value; else SetValue("PasswordType", value);  }
        }
        public string DefaultData
        {
            get { return Simple?item.EditProperty.DefaultData:""; }
            set { if (Simple) item.EditProperty.DefaultData = value; else SetValue("DefaultData", value);}
        }
        [DescriptionAttribute(ULabel.PROPERTY_FIELD_DropStyle_DES)]
        public BillUiFieldsDropListStyle DropStyle
        {
            get { return Simple?item.EditProperty.DropListStyle:null; }
            set {  }
        }
        public BillUiFieldsFormulaProperty FormulaProperty
        {
            get { return Simple?item.FormulaProperty:null; }
            set {  }
        }

        #endregion

        #region 编辑界面

        public bool EditShInGrid
        {
            get { return Simple ? item.EditBorder.ShInGrid : GetBoolValue("EditShInGrid"); }
            set { if (Simple) item.EditBorder.ShInGrid = value; SetValue("EditShInGrid", value); ActiveEvent("EditShInGrid", value); }
        }

        public string EditAlignment
        {
            get { return Simple ? item.EditBorder.Alignment : GetStringValue("EditAlignment"); }
            set { if (Simple) item.EditBorder.Alignment = value; else SetValue("EditAlignment", value);  }
        }
        public int EditColumns
        {
            get { return Simple ? item.EditBorder.Columns : GetIntValue("EditColumns"); }
            set { if (Simple) item.EditBorder.Columns = value; else SetValue("EditColumns", value); }
        }
        public int EditRows
        {
            get { return Simple ? item.EditBorder.Rows : GetIntValue("EditRows") ; }
            set { if (Simple) item.EditBorder.Rows = value; else SetValue("EditRows", value); }
        }
        public bool EditNewLine
        {
            get { return Simple ? item.EditBorder.NewLine : GetBoolValue("EditNewLine") ; }
            set { if (Simple) item.EditBorder.NewLine = value; else SetValue("EditNewLine", value); }
        }
        
        public int EditMaxSize
        {
            get { return Simple ? item.EditBorder.MaxSize : GetIntValue("EditMaxSize"); }
            set { if (Simple) item.EditBorder.MaxSize = value; else SetValue("EditMaxSize", value);}
        }
        public bool EditExpend
        {
            get { return Simple?item.EditBorder.Expend:GetBoolValue("EditExpend"); }
            set { if(Simple)item.EditBorder.Expend = value;else SetValue("EditExpend", value);}
        }
        #endregion

        #region 查询界面
        //----------------------------------------------
        public string ViewAlignment
        {
            get { return Simple?item.ViewBorder.Alignment:GetStringValue("ViewAlignment"); }
            set { if (Simple) item.ViewBorder.Alignment = value; else SetValue("ViewAlignment", value); ActiveEvent("ViewAlignment", value); }
        }
        public int ViewFnWidth
        {
            get { return Simple ? item.ViewBorder.FnWidth : GetIntValue("ViewFnWidth"); }
            set { if(Simple) item.ViewBorder.FnWidth = value; else SetValue("ViewFnWidth", value); }
        }
        public bool ViewShInGrid
        {
            get { return Simple ? item.ViewBorder.ShInGrid : GetBoolValue("ViewShInGrid"); }
            set { if (Simple) item.ViewBorder.ShInGrid = value; else SetValue("ViewShInGrid", value); ActiveEvent("ViewShInGrid", value); }
        }

        public string ViewSubject
        {
            get { return Simple ? item.ViewBorder.Subject : GetStringValue("ViewSubject"); }
            set { if (Simple) item.ViewBorder.Subject = value; else SetValue("ViewSubject", value); }
        }

        public string ViewSummary
        {
            get { return Simple ? item.ViewBorder.Summary : GetStringValue("ViewSummary"); }
            set { if (Simple) item.ViewBorder.Summary = value; else SetValue("ViewSummary", value); }
        }

        public bool FixedColumn
        {
            get { return Simple ? item.ViewBorder.FixedColumn : GetBoolValue("FixedColumn"); }
            set { if (Simple) item.ViewBorder.FixedColumn = value; else SetValue("FixedColumn", value); }
        }

        public bool ViewShSearch
        {
            get { return Simple ? item.ViewBorder.ShSearch : GetBoolValue("ViewShSearch"); }
            set { if (Simple) item.ViewBorder.ShSearch = value; else { SetValue("ViewShSearch", value); } ActiveEvent("ViewShSearch", value); }
        }

        public string ViewSearchType
        {
            get { return Simple ? item.ViewBorder.SearchType : GetStringValue("ViewSearchType"); }
            set { if (Simple) item.ViewBorder.SearchType = value; else SetValue("ViewSearchType", value); }
        }
        public string ViewDefaultData
        {
            get { return Simple ? item.ViewBorder.DefaultData : GetStringValue("ViewDefaultData"); }
            set { if (Simple) item.ViewBorder.DefaultData = value; else SetValue("ViewDefaultData", value); }
        }
        public bool ViewShowMail
        {
            get { return Simple ? item.ViewBorder.ShowMail : GetBoolValue("ViewShowMail"); }
            set { if (Simple) item.ViewBorder.ShowMail = value; else SetValue("ViewShowMail", value); }
        }

        public bool ViewDicMenu
        {
            get { bool val = Simple ? item.ViewBorder.DicMenu : GetBoolValue("ViewDicMenu"); return val; }
            set { if (Simple) item.ViewBorder.DicMenu = value; else SetValue("ViewDicMenu", value); }
        }

        public BillUiFieldsViewBorderLinker ViewLinker
        {
            get { return (Simple)?item.ViewBorder.Linker:null; }
            set { }
        }
        #endregion


        /// <summary>
        /// 触发事件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private void ActiveEvent(string key,object value)
        {
            //logger.Info(string.Format("因字段[{0}]的值改变成[{1}],触发事件", key,Convert.ToString(value)));
            //if (this.DataChange != null)
            //{
            //    PropertyDataEvent args = new PropertyDataEvent(key, value);
            //    this.DataChange(this, args);
            //}
        }

        public BillUiFieldsItem FieldItem
        {
            get { return Simple ? this.item : null; }
        }

        public BillUiFieldToProperty[] Array
        {
            get
            {
                return array;
            }

            set
            {
                array = value;
            }
        }

        public bool Simple
        {
            get
            {
                return simple;
            }

            set
            {
                simple = value;
            }
        }

        private object SetValue(string key, object value)
        {
            
            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {
                    BillUiFieldToProperty data = Array[i];
                    logger.Info(string.Format("修改字段[{0}]的[{1}]值为[{2}]", data.Caption, key, value));
                    Type ClazzType = typeof(BillUiFieldToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null)
                    {
                        Property.SetValue(data, value);
                        logger.Info("修改成功");
                    }
                }
            }
            return value;
        }

        private bool GetBoolValue(string key)
        {
            bool result = true;
            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {

                    BillUiFieldToProperty data = Array[i];
                    Type ClazzType = typeof(BillUiFieldToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(bool))
                    {
                        object val = Property.GetValue(data);
                        if (val.Equals(false))
                        {
                            result = false;
                            break;
                        }
                    }
                }
            }

            return result;
        }

        private int GetIntValue(string key)
        {
            int result = 0;
            if (this.Array != null && this.Array.Length > 0)
            {
                BillUiFieldToProperty data = Array[0];
                Type ClazzType = typeof(BillUiFieldToProperty);
                PropertyInfo Property = ClazzType.GetProperty(key);
                if (Property != null && Property.GetMethod.ReturnType == typeof(int))
                {
                    object val = Property.GetValue(data);
                    if (val is int)
                    {
                        result = (int)val;
                    }
                }
            }
            return result;
        }

        private string GetStringValue(string key)
        {
            string result = "";
            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {
                    BillUiFieldToProperty data = Array[i];
                    Type ClazzType = typeof(BillUiFieldToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(string))
                    {
                        object val = Property.GetValue(data);
                        if (val is string)
                        {
                            if (result == "") result = val.ToString();
                            else if (result != (string)val)
                            {
                                result = "";
                                break;
                            }

                        }
                    }
                }
            }
            return result;
        }


        public bool IsMultiple()
        {
            return !Simple;
        }

        public bool IsTable()
        {
            return Simple && (Table!=null&&Table.Table);
        }

        public bool IsExpendTable()
        {
            return Simple && Table!=null&&Table.Table && Table.Expanded;
        }
    }
}
